package com.example.user.service.impl;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.user.controller.KfakaController;
import com.example.user.dao.ApUserFanMapper;
import com.example.user.dao.ApUserFollowMapper;
import com.example.user.dao.ApUserMapper;
import com.example.user.feign.ArticleFeign;
import com.example.user.service.UserService;
import com.usian.common.kafka.KafkaTopic;
import com.usian.model.article.dtos.ApArticleCheckedDto;
import com.usian.model.article.pojos.ApAuthor;
import com.usian.model.behavior.pojos.ApFollowBehavior;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import com.usian.model.user.pojos.ApUser;
import com.usian.model.user.pojos.ApUserFan;
import com.usian.model.user.pojos.ApUserFollow;
import com.usian.utils.common.SnowflakeIdWorker;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
@Slf4j
public class UserServiceImpl implements UserService {
    @Autowired
    private ArticleFeign articleFeign;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private ApUserFanMapper apUserFanMapper;
    @Autowired
    private ApUserFollowMapper apUserFollowMapper;
    @Autowired
    private ApUserMapper apUserMapper;
    @Autowired
    private KafkaTemplate kfakaController;
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult checkedArticle(ApArticleCheckedDto apArticleCheckedDto) {
        log.info(">>>>>>>>>>>>>>"+apArticleCheckedDto);
        if (apArticleCheckedDto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        ApAuthor apAuthor = articleFeign.getApAuthor(apArticleCheckedDto);
        log.info(">>>>>>>>>>>>>>>>>>"+apAuthor);
        if (apAuthor==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.SIG_ERROR);
        }
        LambdaQueryWrapper<ApUserFan> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ApUserFan::getUserId,apAuthor.getId()).eq(ApUserFan::getFansId,apArticleCheckedDto.getUserId());
        ApUserFan apUserFan = apUserFanMapper.selectOne(queryWrapper);
        log.info(">>>>>>>>>>>>>>>>"+apUserFan);
        if (apUserFan==null && apArticleCheckedDto.getOperticle()==1){
            redisTemplate.boundValueOps(1).set("{\"id\":1,\"salt\":\"abc\",\"name\":\"zhangsan\",\"password\":\"abc\",\"phone\":\"13511223453\",\"image\":null,\"sex\":true,\"certification\":null,\"identityAuthentication\":null,\"status\":true,\"flag\":1,\"createdTime\":\"2020-03-19 23:22:07\"}");
            String apUser = (String) redisTemplate.opsForValue().get(apArticleCheckedDto.getUserId());
            ApUser apUser1 = JSONObject.parseObject(apUser, ApUser.class);
            System.out.println(apUser);
            ApUserFan apUserFan1 = new ApUserFan();
            apUserFan1.setId(Integer.parseInt((SnowflakeIdWorker.UUID()+"").substring(8,13)));
            apUserFan1.setCreatedTime(new Date());
            apUserFan1.setUserId(Integer.parseInt(apAuthor.getId()+""));
            apUserFan1.setFansId(apUser1.getId());
            apUserFan1.setFansName(apUser1.getName());
            apUserFan1.setLevel(Short.parseShort("0"));
            apUserFan1.setIsDisplay(true);
            apUserFan1.setIsShieldComment(false);
            apUserFan1.setIsShieldLetter(false);
            apUserFanMapper.insert(apUserFan1);
            ApUserFollow apUserFollow = new ApUserFollow();
            apUserFollow.setId(Integer.parseInt((SnowflakeIdWorker.UUID()+"").substring(8,13)));
            apUserFollow.setCreatedTime(new Date());
            apUserFollow.setFollowId((int) apAuthor.getId());
            apUserFollow.setFollowName(apAuthor.getName());
            apUserFollow.setUserId(apUser1.getId());
            apUserFollow.setLevel(Short.parseShort("0"));
            apUserFollow.setIsNotice(true);
            apUserFollowMapper.insert(apUserFollow);
            ApFollowBehavior apFollowBehavior = new ApFollowBehavior();
            apFollowBehavior.setId(Long.valueOf((SnowflakeIdWorker.UUID()+"").substring(8,13)));
            apFollowBehavior.setArticleId(apArticleCheckedDto.getArticleId());
            apFollowBehavior.setEntryId(apArticleCheckedDto.getUserId());
            apFollowBehavior.setFollowId(apUserFollow.getId());
            apFollowBehavior.setCreatedTime(new Date());
            String o =JSONObject.toJSONString(apFollowBehavior);
            kfakaController.send(KafkaTopic.TOPIC,o);
            return ResponseResult.okResult(AppHttpCodeEnum.CHECEK_PASS);
        }
        else if (apUserFan!=null && apArticleCheckedDto.getOperticle()==0){
            apUserFanMapper.deleteById(apUserFan.getId());
            LambdaQueryWrapper<ApUserFollow> queryWrapper1 = new LambdaQueryWrapper<>();
            queryWrapper1.eq(ApUserFollow::getUserId,apArticleCheckedDto.getUserId()).eq(ApUserFollow::getFollowId,apUserFan.getUserId());
            apUserFollowMapper.delete(queryWrapper1);
            return ResponseResult.okResult(AppHttpCodeEnum.CHECEK_BLOCK);
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
    }

    @Override
    public ApUser getUserById() {
        return  apUserMapper.selectById(1);
    }
}
